package br.nextfm.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.nextfm.dao.UsuarioDao;
import br.nextfm.model.Administrador;

public class RemoveAdministrador extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int id = Integer.parseInt(request.getParameter("id"));
		
		UsuarioDao dao = new UsuarioDao();
		
		HttpSession session = request.getSession(false);
		
		Administrador logado = (Administrador) session.getAttribute("administrador");
		
		if (dao.getAdministrador(id).getId() != logado.getId()) {
			
			if (!dao.deleteAdministrador(id)) {

				request.setAttribute("erro", true);
				
			}
		
		}
		
		else {

			request.setAttribute("erro", true);
			
		}
		
		request.setAttribute("administradores", dao.getAllAdministradores());
		request.getRequestDispatcher("administrador.jsp").forward(request, response);
		
	}

}
